<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_from"
          :model="loginForm"
          :rules="rules"
          ref="loginFroms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到东方甑选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              show-password
              type="password"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_bth"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue"
import { reactive, ref } from "vue"
import { useRouter, useRoute } from "vue-router"
import useUserStore from "../../store/modules/user"
import { ElNotification } from "element-plus"
import { getTime } from "../../utils/time"

//收集账号密码数据
let loginForm = reactive({ username: "admin", password: "111111" })
let userStore = useUserStore()
let $router = useRouter()
let $route = useRoute()
//控制按钮加载
let loading = ref(false)
let loginFroms = ref()

const login = async () => {
  await loginFroms.value.validate()
  //开始加载
  loading.value = true
  //通知仓库发登录请求
  //成功 跳转首页 展示数据
  //失败 弹出失败信息
  try {
    await userStore.userLogin(loginForm)
    //编程时导航跳转展示数据首页
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || "/" })
    //提示信息
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `Hi,${getTime()}好`,
    })
  } catch (error) {
    ElNotification({ type: "error", message: (error as Error).message })
  } finally {
    loading.value = false
  }
}

const validatorUserName = (_rule: any, value: any, callback: any) => {
  //函数：如果符合条件callback放行
  if (/^\d{5,15}$/.test(value)) {
    callback()
  } else {
    callback(new Error("账号长度至少5为"))
  }
}

//定义表单校验需要的配置
const rules = {
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
    {
      required: true,
      message: "账号字符长度5~10位",
      min: 5,
      max: 10,
      trigger: "change",
    },
  ],
  password: [
    {
      trigger: "change",
      validator: validatorUserName,
    },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("../../assets/images/background.jpg") no-repeat;
  background-size: cover;
}
.login_from {
  position: relative;
  width: 55%;
  top: 30vh;
  background: url("../../assets/images/login_form.png") no-repeat;
  background-size: cover;
  padding: 40px;
  h1 {
    color: white;
    font-size: 40px;
  }
  h2 {
    color: white;
    font-size: 20px;
    margin: 20px;
  }
  .login_bth {
    width: 100%;
  }
}
</style>
